package com.controller;

import com.mapper.GoodsSKUMapper;
import com.pojo.GoodsSKU;
import com.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 用于初始化redis数据库
 * @author zzuli_huahua
 * @version 1.0
 * @date 2022/7/9 20:28
 */
@RestController
@Slf4j
public class redisController {

    @Resource
    RedisUtil redisUtil;

    @Resource
    private GoodsSKUMapper goodsSKUMapper;

    /**
     * 根据商品的主键ID查询库存量，走索引 查询快
     * 将数据库的库存读取到 redis 提前把库存放到缓存
     * http://localhost:8080/initRedis
     * @return 下单结果
     */
    @GetMapping("initRedis")
    public String initRedis(){
        //1、查询商品库存
        GoodsSKU goodsSKU = goodsSKUMapper.selectById(8002);
        //2、将库存写入redis缓存中
        try {
            log.info("库存键值对=====》"+goodsSKU.getGoodsId()+"------>"+goodsSKU.getNum());
            redisUtil.set(String.valueOf(goodsSKU.getGoodsId()),goodsSKU.getNum());
        }catch (Exception e){
            return "init Redis failed！";
        }
        return "init Redis succeed！";
    }

}
